//
//  Framework for a raytracer
//  File: plan.cpp
//
//  Created for the Computer Science course "Introduction Computer Graphics"
//  taught at the University of Groningen by Tobias Isenberg.
//
//  Authors:
//	  Zhe Sun
//
//  This framework is inspired by and uses code of the raytracer framework of 
//  Bert Freudenberg that can be found at
//  http://isgwww.cs.uni-magdeburg.de/graphik/lehre/cg2/projekt/rtprojekt.html 
//

#include "geometric.h"
#include <iostream>
#include <math.h>

/************************** Plan **********************************/

Hit Plan::intersect(const Ray &ray)
{
     // All code below is based on http://www.cl.cam.ac.uk/teaching/1999/AGraphHCI/SMAG/node2.html
    
    // ray is parallet to the plan
    //if (fabs(ray.O.dot(ray.D)) < 1e-6) {
    if (fabs(ray.O.dot(ray.D)) < 1e-6) {
      return Hit::NO_HIT();
    }
    
    double t = normal.dot(point - ray.O)/(normal.dot(ray.D));

    // plan is behind the eye point and there is no intersection
    if ( t < 0 ) {
      return Hit::NO_HIT();
    }
    
    // otherwise, intersect and return t and N
    return Hit(t, normal);
}


void Plan::mapCoord(Point p, double* u, double* v) {
	double width = 2400;
	double height = 1600;
  
	
	*u = (p.x + 1000) / width;
	*v = (p.y + 600) / height;
	
	if(*u < 0 ) {
	  *u = 0.0;
	}	
	if (*u > 1.0){
	  *u = 0.99;
	}
	if (*v < 0 ) {
	  *v = 0.0;
	}
	if ( *v > 1.0) {
	  *v = 0.99;
	}
}

void Plan::updateMotionPostion(Vector v, double speed, double t){
  point += t * speed * v;
}
